﻿using Abp.Domain.Repositories;
using Abp.UI;
using SOEI.Solution.CommonOthers.BarCode.StructEntities;
using SOEI.TxmApi.Extend.Core;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace SOEI.Solution.CommonOthers.BarCode.DomainService
{
    public class BarCodeManager : DomainServiceBase, IBarCodeManager
    {
        public readonly IRepository<Default> _repository;

        public BarCodeManager(IRepository<Default> repository)
        {
            _repository = repository;
        }

        /// <summary>获取二维码图片
        /// </summary>
        public async Task<byte[]> GetBarCodeImage(BarCodeParam pParam)
        {
            if(pParam == null)
            {
                throw new UserFriendlyException("参数不能为空！");
            }

            string vEWMBH = pParam.EWMBH;

            var vTxmContext = TxmContext.This;

            if (string.IsNullOrWhiteSpace(vEWMBH))
            {
                vEWMBH = vTxmContext.GetBarCodeBH(pParam.YWDM, pParam.YWBH);
            }

            var vUrl = vTxmContext.GetBarCodeSrc(vEWMBH);

            byte[] vData = vTxmContext.DownLoadHttpFileToBytes(vUrl);

            return await Task.FromResult(vData);
        }

        /// <summary>根据参数创建二维码
        /// </summary>
        public async Task<string> CreateBarCode(BarCodeParam pParam)
        {
            if(pParam == null)
            {
                throw new UserFriendlyException("参数不能为空！");
            }

            var res = TxmContext.This.CreateBarCode(pParam.YWDM,
                                                    pParam.YWLXDM,
                                                    pParam.YWLXMC,
                                                    pParam.SCR,
                                                    pParam.YWBH,
                                                    pParam.YWCXSJ,
                                                    pParam.SFXSYWMBH,
                                                    pParam.CZEWMSFSC,
                                                    pParam.URL);

            return await Task.FromResult(res);
        }

    }
}
